﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
using BaHuan.Reports;
namespace BaHuan
{
    public partial class FrmXemReport : Form
    {
        private ConnectionInfo crConnectionInfo = new ConnectionInfo();
        private TableLogOnInfo crTableLogonInfo = new TableLogOnInfo();
        private Sections crSections;
        private ReportObjects crReportObjects;
        private SubreportObject crSubreportObject;
        private ReportDocument crSubreportDocument;
        private Database crDatabase;

        public event FormCloseXemReportEventHandler formclosexemreport = null;

        private PhieuChiTienManagement phieuchitienMgt = new PhieuChiTienManagement();
        private PhieuThuTienManagement phieuthutienMgt = new PhieuThuTienManagement();
        private MuaHangManagement muahangMgt = new MuaHangManagement();
        private PhieuKeToanManagement phieuketoanMgt = new PhieuKeToanManagement();
        private PhieuChuyenKhoManagement phieuchuyenkhoMgt = new PhieuChuyenKhoManagement();
        private PhieuDoiHangManagement phieudoihangMgt = new PhieuDoiHangManagement();
        private PhieuHangTraManagement phieuhangtraMgt = new PhieuHangTraManagement();
        private PhieuNhapKhoMgt phieunhapkhoMgt = new PhieuNhapKhoMgt();
        private PhieuXuatHangManagement phieuxuathangMgt = new PhieuXuatHangManagement();
        private CTPhieuXuatHangManagement ctphieuxuathangMgt = new CTPhieuXuatHangManagement();
        private PhieuXuatKhoManagement PhieuXuatKhoMgt = new PhieuXuatKhoManagement();
        private UyNhiemChiManagement uynhiemchiMgt = new UyNhiemChiManagement();

        private int cnt = 0;
        public NhanVien nhanvien = new NhanVien();

        public FrmXemReport()
        {
            InitializeComponent();
        }

        public void SetConnection()
        {
            //"Data Source=.\SQLEXPRESS;Initial Catalog=BaHuan;Integrated Security=True"
            //crConnectionInfo.ServerName = @".\SQLEXPRESS";
            //crConnectionInfo.DatabaseName = "BaHuan";
            //crConnectionInfo.IntegratedSecurity = true;

            crConnectionInfo.ServerName = "14.161.25.119";
            crConnectionInfo.DatabaseName = "BaHuan";
            crConnectionInfo.UserID = "developer";
            crConnectionInfo.Password = "abc123";
        }

        /// <summary>
        /// Set value for parameter and call report BangKeNhapHang
        /// </summary>
        /// <param name="bangkenhaphang"></param>
        public FrmXemReport(BangKeNhapHang bangkenhaphang)
        {
            InitializeComponent();
            cnt = 1;
            SetConnection();

            rptBangKeNhapHang rptBK = new rptBangKeNhapHang();
            Tables CrTables;

            CrTables = rptBK.Database.Tables;
            foreach (Table CrTable in CrTables)
            {
                crTableLogonInfo = CrTable.LogOnInfo;
                crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crTableLogonInfo);
            }
            
            rptBK.SetParameterValue("Id", bangkenhaphang.Id);
            crvView.ReportSource = rptBK;
        }

        /// <summary>
        /// Set value for parameter and call report PhieuNhapKho
        /// </summary>
        /// <param name="phieunhapkho"></param>
        public FrmXemReport(PhieuNhapKho phieunhapkho, string tienchu, long tienso)
        {
            InitializeComponent();
            cnt = 2;
            SetConnection();

            rptPhieuNhapKho rptPNK = new rptPhieuNhapKho();
            Tables CrTables;

            CrTables = rptPNK.Database.Tables;
            foreach (Table CrTable in CrTables)
            {
                crTableLogonInfo = CrTable.LogOnInfo;
                crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crTableLogonInfo);
            }

            rptPNK.SetParameterValue("Id", phieunhapkho.Id);
            rptPNK.SetParameterValue("TienChu", tienchu);
            rptPNK.SetParameterValue("TongTien",tienso);
            rptPNK.SetParameterValue("sochungtu", phieunhapkho.MaSo, "PNK_TKNO");
            crvView.ReportSource = rptPNK;         
        }

        /// <summary>
        /// Set value for parameter and call report PhieuXuatKho
        /// </summary>
        /// <param name="phieuxuatkho"></param>
        public FrmXemReport(int idPhieuXuatKho)
        {
            InitializeComponent();
            cnt = 3;
            SetConnection();

            rptPhieuXuatKho rptPXK = new rptPhieuXuatKho();
            Tables CrTables;

            CrTables = rptPXK.Database.Tables;
            foreach (Table CrTable in CrTables)
            {
                crTableLogonInfo = CrTable.LogOnInfo;
                crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crTableLogonInfo);
            }

            rptPXK.SetParameterValue("Id", idPhieuXuatKho);
            rptPXK.SetParameterValue("Id", idPhieuXuatKho, "subPXK");
            crvView.ReportSource = rptPXK;
        }

        /// <summary>
        /// Set value for parameter and call report PhieuChuyenKho
        /// </summary>
        /// <param name="phieuchuyenkho"></param>
        public FrmXemReport(PhieuChuyenKho phieuchuyenkho)
        {
            InitializeComponent();
            cnt = 4;
            SetConnection();

            rptPhieuChuyenKho rptPCK = new rptPhieuChuyenKho();
            Tables CrTables;

            CrTables = rptPCK.Database.Tables;
            foreach (Table CrTable in CrTables)
            {
                crTableLogonInfo = CrTable.LogOnInfo;
                crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crTableLogonInfo);
            }

            rptPCK.SetDataSource(phieuchuyenkho);
            rptPCK.SetParameterValue("Id", phieuchuyenkho.Id, "PCK_HH");
            rptPCK.SetParameterValue("Id", phieuchuyenkho.Id, "PCK_BB");
            crvView.ReportSource = rptPCK;
        }

        /// <summary>
        /// Set value for parameter and call report PhieuChuanBiXuatKho
        /// </summary>
        /// <param name="phieuxuathang"></param>
        public FrmXemReport(PhieuXuatHang phieuxuathang)
        {
            InitializeComponent();
            cnt = 5;
            SetConnection();

            rptPhieuChuanBiXuatKho rpt = new rptPhieuChuanBiXuatKho();
            Tables CrTables;

            CrTables = rpt.Database.Tables;
            foreach (Table CrTable in CrTables)
            {
                crTableLogonInfo = CrTable.LogOnInfo;
                crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crTableLogonInfo);
            }

            rpt.SetParameterValue("Id", phieuxuathang.Id);
            crvView.ReportSource = rpt;
        }

        /// <summary>
        /// Set value for parameter and call report PhieuXuatHang
        /// </summary>
        /// <param name="phieuxuathang"></param>
        public FrmXemReport(PhieuXuatHang phieuxuathang, string tienchu)
        {
            InitializeComponent();
            cnt = 6;
            SetConnection();

            rptPhieuXuatHang rptPXH = new rptPhieuXuatHang();
            Tables CrTables;

            CrTables = rptPXH.Database.Tables;
            foreach (Table CrTable in CrTables)
            {
                crTableLogonInfo = CrTable.LogOnInfo;
                crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crTableLogonInfo);
            }

            rptPXH.SetParameterValue("Id", phieuxuathang.Id);
            rptPXH.SetParameterValue("TienChu", tienchu);
            rptPXH.SetParameterValue("Id", phieuxuathang.Id, "PXH_HH");
            rptPXH.SetParameterValue("Id", phieuxuathang.Id, "PXH_BB");
            crvView.ReportSource = rptPXH;
        }

        /// <summary>
        /// Set value for parameter and call report HoaDonDo
        /// </summary>
        /// <param name="phieuxuathang"></param>
        /// <param name="sTienChu"></param>
        public FrmXemReport(PhieuXuatHang phieuxuathang, string sTienChu, bool IsHDD)
        {
            InitializeComponent();
            cnt = 7;
            SetConnection();

            rptHoaDonDo rptHDD = new rptHoaDonDo();
            Tables CrTables;

            CrTables = rptHDD.Database.Tables;
            foreach (Table CrTable in CrTables)
            {
                crTableLogonInfo = CrTable.LogOnInfo;
                crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crTableLogonInfo);
            }

            rptHDD.SetParameterValue("Id", phieuxuathang.Id);
            rptHDD.SetParameterValue("TienChu", sTienChu);
            crvView.ReportSource = rptHDD;
        }

        /// <summary>
        /// Set value for parameter and call report PhieuChiTien
        /// </summary>
        /// <param name="phieuchitien"></param>
        /// <param name="sLoaiPhieuChi"></param>
        /// <param name="tienchu"></param>
        /// <param name="diachi"></param>
        public FrmXemReport(PhieuChiTien phieuchitien, string sLoaiPhieuChi, string tienchu, string diachi)
        {
            InitializeComponent();
            cnt = 8;
            SetConnection();

            rptPhieuChi rptPC = new rptPhieuChi();
            Tables CrTables;

            CrTables = rptPC.Database.Tables;
            foreach (Table CrTable in CrTables)
            {
                crTableLogonInfo = CrTable.LogOnInfo;
                crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crTableLogonInfo);
            }

            crSections = rptPC.ReportDefinition.Sections;
            foreach (Section crSection in crSections)
            {
                crReportObjects = crSection.ReportObjects;
                //loop through all the report objects in there to find all subreports 
                foreach (ReportObject crReportObject in crReportObjects)
                {
                    if (crReportObject.Kind == ReportObjectKind.SubreportObject)
                    {
                        crSubreportObject = (SubreportObject)crReportObject;
                        //open the subreport object and logon as for the general report 
                        crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
                        crDatabase = crSubreportDocument.Database;
                        CrTables = crDatabase.Tables;
                        foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in CrTables)
                        {
                            crTableLogonInfo = aTable.LogOnInfo;
                            crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                            aTable.ApplyLogOnInfo(crTableLogonInfo);
                        }
                    }
                }
            }

            rptPC.SetParameterValue("LoaiPC", sLoaiPhieuChi);
            rptPC.SetParameterValue("MaSo", phieuchitien.MaSo);
            rptPC.SetParameterValue("Ngay", phieuchitien.NgayThang);
            rptPC.SetParameterValue("NguoiNhanTien", phieuchitien.NguoiNhanTien);
            rptPC.SetParameterValue("ChungTuKemTheo", phieuchitien.ChungTuKemTheo);
            rptPC.SetParameterValue("LyDo", phieuchitien.LyDo);
            rptPC.SetParameterValue("SoTien", phieuchitien.SoTien);
            rptPC.SetParameterValue("TienChu", tienchu);
            rptPC.SetParameterValue("DonVi", phieuchitien.DoiTuong);
            rptPC.SetParameterValue("DiaChi", diachi);
            rptPC.SetParameterValue("MSCT", phieuchitien.MaSo, "subPC");
            crvView.ReportSource = rptPC;
        }

        /// <summary>
        /// Set value for parameter and call report PhieuThuTien
        /// </summary>
        /// <param name="phieuthutien"></param>
        /// <param name="sLoaiPhieuThu"></param>
        /// <param name="tienchu"></param>
        /// <param name="diachi"></param>
        public FrmXemReport(PhieuThuTien phieuthutien, string sLoaiPhieuThu, string tienchu, string diachi)
        {
            InitializeComponent();
            cnt = 9;
            SetConnection();

            rptPhieuThu rptPT = new rptPhieuThu();
            Tables CrTables;

            CrTables = rptPT.Database.Tables;
            foreach (Table CrTable in CrTables)
            {
                crTableLogonInfo = CrTable.LogOnInfo;
                crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crTableLogonInfo);
            }

            crSections = rptPT.ReportDefinition.Sections;
            foreach (Section crSection in crSections)
            {
                crReportObjects = crSection.ReportObjects;
                //loop through all the report objects in there to find all subreports 
                foreach (ReportObject crReportObject in crReportObjects)
                {
                    if (crReportObject.Kind == ReportObjectKind.SubreportObject)
                    {
                        crSubreportObject = (SubreportObject)crReportObject;
                        //open the subreport object and logon as for the general report 
                        crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
                        crDatabase = crSubreportDocument.Database;
                        CrTables = crDatabase.Tables;
                        foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in CrTables)
                        {
                            crTableLogonInfo = aTable.LogOnInfo;
                            crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                            aTable.ApplyLogOnInfo(crTableLogonInfo);
                        }
                    }
                }
            }

            rptPT.SetParameterValue("LoaiPT", sLoaiPhieuThu);
            rptPT.SetParameterValue("MaSo", phieuthutien.MaSo);
            rptPT.SetParameterValue("Ngay", phieuthutien.NgayThang);
            rptPT.SetParameterValue("NguoiNopTien", phieuthutien.NguoiNopTien);
            rptPT.SetParameterValue("ChungTuKemTheo", phieuthutien.ChungTuKemTheo);
            rptPT.SetParameterValue("LyDo", phieuthutien.LyDo);
            rptPT.SetParameterValue("SoTien", phieuthutien.SoTien);
            rptPT.SetParameterValue("TienChu", tienchu);
            rptPT.SetParameterValue("DonVi", phieuthutien.DoiTuong);
            rptPT.SetParameterValue("DiaChi", diachi);
            rptPT.SetParameterValue("MaSo", phieuthutien.MaSo, "subPT");
            crvView.ReportSource = rptPT;
        }

        /// <summary>
        /// Set value for parameter and call report SoKeToan
        /// </summary>
        public FrmXemReport(string tentaikhoan, string nguoilap, List<ButToan> lst, DateTime dtTuNgay, DateTime dtDenNgay, long SDDKNo, long SDDKCo, long PSNo, long PSCo, long SDCKNo, long SDCKCo, string SoTK, string DoiTuong)
        {
            InitializeComponent();
            cnt = 10;

            rptSoKeToan rptSoKT = new rptSoKeToan();

            rptSoKT.SetDataSource(lst);
            rptSoKT.SetParameterValue("TenTaiKhoan", tentaikhoan);
            rptSoKT.SetParameterValue("NguoiLap", nguoilap);
            rptSoKT.SetParameterValue("TuNgay", dtTuNgay);
            rptSoKT.SetParameterValue("DenNgay", dtDenNgay);
            rptSoKT.SetParameterValue("SDDKNo", SDDKNo);
            rptSoKT.SetParameterValue("SDDKCo", SDDKCo);
            rptSoKT.SetParameterValue("PSNo", PSNo);
            rptSoKT.SetParameterValue("PSCo", PSCo);
            rptSoKT.SetParameterValue("SDCKNo", SDCKNo);
            rptSoKT.SetParameterValue("SDCKCo", SDCKCo);
            rptSoKT.SetParameterValue("SoTK", SoTK);
            rptSoKT.SetParameterValue("DoiTuong", DoiTuong);

            crvView.ReportSource = rptSoKT;
        }

        /// <summary>
        /// Set value for parameter and call report TheKho
        /// </summary>
        /// <param name="lst"></param>
        /// <param name="sTenHang"></param>
        /// <param name="sMaSo"></param>
        /// <param name="lTonDauKy"></param>
        public FrmXemReport(List<TheKho> lst, string sTenHang, string sMaSo, long lTonDauKy)
        {
            InitializeComponent();
            cnt = 11;

            rptTheKho rptTK = new rptTheKho();

            rptTK.SetDataSource(lst);
            rptTK.SetParameterValue("tondauky", lTonDauKy);
            rptTK.SetParameterValue("TenVatTu", sTenHang);
            rptTK.SetParameterValue("MaVatTu", sMaSo);

            crvView.ReportSource = rptTK;
        }

        /// <summary>
        /// Set value for parameter and call report TheKho
        /// </summary>
        /// <param name="lst"></param>
        /// <param name="dtTuNgay"></param>
        /// <param name="dtDenNgay"></param>
        public FrmXemReport(List<BangCanDoiXuatNhapTon> lstPhatSinh, DateTime TuNgay, DateTime DenNgay, string sKho)
        {
            InitializeComponent();
            cnt = 12;

            rptBCDXNT rptXNT = new rptBCDXNT();

            rptXNT.SetDataSource(lstPhatSinh);
            rptXNT.SetParameterValue("TuNgay", TuNgay);
            rptXNT.SetParameterValue("DenNgay", DenNgay);
            rptXNT.SetParameterValue("Kho", sKho);

            crvView.ReportSource = rptXNT;
        }

        /// <summary>
        /// Set value for parameter and call report TheKho
        /// </summary>
        /// <param name="lst"></param>
        /// <param name="dtTuNgay"></param>
        /// <param name="dtDenNgay"></param>
        public FrmXemReport(List<TongHopNhapKho> lst, DateTime dtTuNgay, DateTime dtDenNgay)
        {
            InitializeComponent();
            cnt = 13;

            rptBangTongHopMuaHang rptTH = new rptBangTongHopMuaHang();

            rptTH.SetDataSource(lst);
            rptTH.SetParameterValue("TuNgay", dtTuNgay);
            rptTH.SetParameterValue("DenNgay", dtDenNgay);

            crvView.ReportSource = rptTH;
        }

        /// <summary>
        /// Set value for parameter and call report TheKho
        /// </summary>
        /// <param name="lst"></param>
        /// <param name="dtTuNgay"></param>
        /// <param name="dtDenNgay"></param>
        public FrmXemReport(List<TongHopHangTra> lst, DateTime dtTuNgay, DateTime dtDenNgay)
        {
            InitializeComponent();
            cnt = 14;

            rptLietKeNhapHang rpt = new rptLietKeNhapHang();

            rpt.SetDataSource(lst);
            rpt.SetParameterValue("TuNgay", dtTuNgay);
            rpt.SetParameterValue("DenNgay", dtDenNgay);

            crvView.ReportSource = rpt;
        }

        /// <summary>
        /// Set value for parameter and call report TheKho
        /// </summary>
        /// <param name="lst"></param>
        /// <param name="dtTuNgay"></param>
        /// <param name="dtDenNgay"></param>
        public FrmXemReport(List<TongHopChuyenKho> lst, DateTime dtTuNgay, DateTime dtDenNgay)
        {
            InitializeComponent();
            cnt = 15;

            rptLietKeChuyenKho rptCK = new rptLietKeChuyenKho();

            rptCK.SetDataSource(lst);
            rptCK.SetParameterValue("TuNgay", dtTuNgay);
            rptCK.SetParameterValue("DenNgay", dtDenNgay);

            crvView.ReportSource = rptCK;
        }

        /// <summary>
        /// Set value for parameter and call report KhauTruThue
        /// </summary>
        public FrmXemReport(string sThoiGian, long lSDDK1331, long lSDDK3331, long lTien1331, long lTien3331, long lSoNopTrongKy, long lSoConDuocKhauTru)
        {
            InitializeComponent();
            cnt = 16;

            rptKhauTruThue rptKTT = new rptKhauTruThue();

            rptKTT.SetParameterValue("ThoiGian", sThoiGian);
            rptKTT.SetParameterValue("SDDK1331", lSDDK1331);
            rptKTT.SetParameterValue("SDDK3331", lSDDK3331);
            rptKTT.SetParameterValue("Tien1331", lTien1331);
            rptKTT.SetParameterValue("Tien3331", lTien3331);
            rptKTT.SetParameterValue("SoNopTrongKy", lSoNopTrongKy);
            rptKTT.SetParameterValue("SoConDuocKhauTru", lSoConDuocKhauTru);
            crvView.ReportSource = rptKTT;
        }

        /// <summary>
        /// Set value for parameter and call report PhieuThanhToanTienHang
        /// </summary>
        /// <param name="lst"></param>
        public FrmXemReport(List<CTPhieuXuatHang> lstCTPXH, List<CTPhieuHangTra> lstCTPHT, long lSoDu, long lThue, long lThanhToan, long lChieuKhau, long lHangBan, long lHangTra, DateTime sThoiGian, string sTienChu,string sTenKH,string sMSKH)
        {            
            InitializeComponent();
            cnt = 17;

            rptPhieuThanhToanTienHang rptPTTTH = new rptPhieuThanhToanTienHang();

            if (lstCTPXH.Count > 0)
                rptPTTTH.OpenSubreport("subHangBan").SetDataSource(lstCTPXH);
            if (lstCTPHT.Count > 0)
                rptPTTTH.OpenSubreport("subHangTra").SetDataSource(lstCTPHT);

            rptPTTTH.SetParameterValue("DuNoDauKy",lSoDu);
            rptPTTTH.SetParameterValue("Thue", lThue);
            rptPTTTH.SetParameterValue("ThanhToan", lThanhToan);
            rptPTTTH.SetParameterValue("ChietKhau", lChieuKhau);
            rptPTTTH.SetParameterValue("HangBan", lHangBan);
            rptPTTTH.SetParameterValue("HangTra", lHangTra);
            rptPTTTH.SetParameterValue("Ngay", sThoiGian);
            rptPTTTH.SetParameterValue("TienChu", sTienChu);
            rptPTTTH.SetParameterValue("TenKH", sTenKH);
            rptPTTTH.SetParameterValue("MSKH", sMSKH);
            crvView.ReportSource = rptPTTTH;
        }

        /// <summary>
        /// Set value for parameter and call report BCDKT
        /// </summary>
        /// <param name="lst"></param>
        public FrmXemReport(List<BangCanDoiKeToan> lst)
        {
            InitializeComponent();
            cnt = 18;

            rptBCDKT rpt = new rptBCDKT();

            rpt.SetDataSource(lst);

            crvView.ReportSource = rpt;
        }

        /// <summary>
        /// Set value for parameter and call report BCDTK
        /// </summary>
        /// <param name="lst"></param>
        public FrmXemReport(List<BangCanDoiTaiKhoan> lst, DateTime dtTungay, DateTime dtDenNgay)
        {
            InitializeComponent();
            cnt = 19;

            rptBCDTK rpt = new rptBCDTK();

            if(lst.Count>0)
                rpt.SetDataSource(lst);

            rpt.SetParameterValue("TuNgay", dtTungay);
            rpt.SetParameterValue("DenNgay", dtDenNgay);
            crvView.ReportSource = rpt;
        }

        /// <summary>
        /// Set value for parameter and call report BCQ
        /// </summary>
        /// <param name="lst"></param>
        public FrmXemReport(List<BaoCaoQuy> lst, string tendoituong, string tentaikhoan, DateTime dtTuNgay, DateTime dtDenNgay, long DuNo, long DuCo)
        {
            InitializeComponent();
            cnt = 20;

            rptBaoCaoQuy rpt = new rptBaoCaoQuy();

            rpt.SetDataSource(lst);
            rpt.SetParameterValue("TenDoiTuong", tendoituong);
            rpt.SetParameterValue("TenTaiKhoan", tentaikhoan);
            rpt.SetParameterValue("TuNgay", dtTuNgay);
            rpt.SetParameterValue("DenNgay", dtDenNgay);
            rpt.SetParameterValue("DuNo", DuNo);
            rpt.SetParameterValue("DuCo", DuCo);

            crvView.ReportSource = rpt;
        }

        /// <summary>
        /// Set value for parameter and call report BCQTH
        /// </summary>
        /// <param name="lst"></param>
        public FrmXemReport(List<BaoCaoQuyTongHop> lst, string tendoituong, string tentaikhoan, DateTime dtTuNgay, DateTime dtDenNgay)
        {
            InitializeComponent();
            cnt = 21;

            rptBaoCaoQuyTongHop rpt = new rptBaoCaoQuyTongHop();

            rpt.SetDataSource(lst);
            rpt.SetParameterValue("TenTK", tentaikhoan);
            rpt.SetParameterValue("TuNgay", dtTuNgay);
            rpt.SetParameterValue("DenNgay", dtDenNgay);

            crvView.ReportSource = rpt;
        }

        /// <summary>
        /// Set value for parameter and call report CongNoTongHop131
        /// </summary>
        /// <param name="lst"></param>
        public FrmXemReport(List<TongHopCongNo131> lst, DateTime dtTungay, DateTime dtDenNgay)
        {
            InitializeComponent();
            cnt = 22;

            rptTHCongNo131 rpt = new rptTHCongNo131();

            rpt.SetDataSource(lst);
            rpt.SetParameterValue("TuNgay", dtTungay);
            rpt.SetParameterValue("DenNgay", dtDenNgay);
            crvView.ReportSource = rpt;
        }

        /// <summary>
        /// Set value for parameter and call report CongNoTongHop331
        /// </summary>
        /// <param name="lst"></param>
        public FrmXemReport(List<TongHopCongNo331> lst, DateTime dtTungay, DateTime dtDenNgay)
        {
            InitializeComponent();
            cnt = 23;

            rptTHCongNo331 rpt = new rptTHCongNo331();

            rpt.SetDataSource(lst);
            rpt.SetParameterValue("TuNgay", dtTungay);
            rpt.SetParameterValue("DenNgay", dtDenNgay);
            crvView.ReportSource = rpt;
        }

        /// <summary>
        /// Set value for parameter and call report CTCongNo
        /// </summary>
        /// <param name="lst331"></param>
        public FrmXemReport(List<ButToan> lst, DateTime dtTungay, DateTime dtDenNgay, long DuNo, long DuCo, string DoiTuong, int type)
        {
            // Type 0 = TK 131
            // Type 1 = TK 331

            InitializeComponent();
            cnt = 24;

            if (type == 0)
            {
                rptCTCongNo131 rpt = new rptCTCongNo131();
                rpt.SetDataSource(lst);
                rpt.SetParameterValue("TuNgay", dtTungay);
                rpt.SetParameterValue("DenNgay", dtDenNgay);
                rpt.SetParameterValue("DuNo", DuNo);
                rpt.SetParameterValue("DuCo", DuCo);
                rpt.SetParameterValue("DoiTuong", DoiTuong);
                crvView.ReportSource = rpt;
            }
            else
            {
                rptCTCongNo331 rpt = new rptCTCongNo331();
                rpt.SetDataSource(lst);
                rpt.SetParameterValue("TuNgay", dtTungay);
                rpt.SetParameterValue("DenNgay", dtDenNgay);
                rpt.SetParameterValue("DuNo", DuNo);
                rpt.SetParameterValue("DuCo", DuCo);
                rpt.SetParameterValue("DoiTuong", DoiTuong);
                crvView.ReportSource = rpt;
            }
        }

        /// <summary>
        /// Set value for parameter and call report UyNhiemChi
        /// </summary>
        /// <param name=""></param>
        public FrmXemReport(string tienchu, string mauin, string donvichuyen, string tkchuyen, string nganhangchuyen, string donvinhan, string tknhan, string nganhangnhan, string diachinganhangnhan, string noidung, long sotien)
        {
            InitializeComponent();
            cnt = 25;

            if (mauin == "Agribank")
            {
                rptPO_Agribank rpt = new rptPO_Agribank();

                rpt.SetParameterValue("DonViChuyen", donvichuyen);
                rpt.SetParameterValue("TKChuyen", tkchuyen);
                rpt.SetParameterValue("NganHangChuyen", nganhangchuyen);
                rpt.SetParameterValue("DonViNhan", donvinhan);
                rpt.SetParameterValue("TKNhan", tknhan);
                rpt.SetParameterValue("NganHangNhan", nganhangnhan);
                rpt.SetParameterValue("DiaDiemNHNhan", diachinganhangnhan);
                rpt.SetParameterValue("TienChu", tienchu);
                rpt.SetParameterValue("NoiDung", noidung);
                rpt.SetParameterValue("SoTien", sotien);

                crvView.ReportSource = rpt;
            }
            else if (mauin == "BIDV")
            {
                rptPO_BIDV rpt = new rptPO_BIDV();

                rpt.SetParameterValue("DonViChuyen", donvichuyen);
                rpt.SetParameterValue("TKChuyen", tkchuyen);
                rpt.SetParameterValue("NganHangChuyen", nganhangchuyen);
                rpt.SetParameterValue("DonViNhan", donvinhan);
                rpt.SetParameterValue("TKNhan", tknhan);
                rpt.SetParameterValue("NganHangNhan", nganhangnhan);
                rpt.SetParameterValue("TienChu", tienchu);
                rpt.SetParameterValue("NoiDung", noidung);
                rpt.SetParameterValue("SoTien", sotien);

                crvView.ReportSource = rpt;
            }
            else if (mauin == "Sacombank")
            {
                rptPO_Sacombank rpt = new rptPO_Sacombank();

                rpt.SetParameterValue("DonViChuyen", donvichuyen);
                rpt.SetParameterValue("TKChuyen", tkchuyen);
                rpt.SetParameterValue("NganHangChuyen", nganhangchuyen);
                rpt.SetParameterValue("DonViNhan", donvinhan);
                rpt.SetParameterValue("TKNhan", tknhan);
                rpt.SetParameterValue("NganHangNhan", nganhangnhan);
                rpt.SetParameterValue("DiaDiemNHNhan", diachinganhangnhan);
                rpt.SetParameterValue("TienChu", tienchu);
                rpt.SetParameterValue("NoiDung", noidung);
                rpt.SetParameterValue("SoTien", sotien);

                crvView.ReportSource = rpt;
            }
        }

        private void crvView_ClickPage(object sender, CrystalDecisions.Windows.Forms.PageMouseEventArgs e)
        {
            if (e.ObjectInfo.Name == "PhieuChi" && cnt == 20)
            {
                PhieuChiTien phieuchitien = (from c in GetAllPhieuChiTien()
                                             where c.MaSo == e.ObjectInfo.Text
                                             select c).FirstOrDefault();

                FrmPhieuChiTien frmPhieuChiTien = new FrmPhieuChiTien(phieuchitien, this.nhanvien);
                this.Hide();
                frmPhieuChiTien.ShowDialog();
            }
            else if (e.ObjectInfo.Name == "PhieuThu" && cnt == 20)
            {
                PhieuThuTien phieuthutien = (from c in GetAllPhieuThuTien()
                                             where c.MaSo == e.ObjectInfo.Text
                                             select c).FirstOrDefault();

                FrmPhieuThuTien frmPhieuThuTien = new FrmPhieuThuTien(phieuthutien, this.nhanvien);
                this.Hide();
                frmPhieuThuTien.ShowDialog();
            }
            else if (e.ObjectInfo.Name == "SoChungTu" && cnt == 10)
            {
                string[] maso = e.ObjectInfo.Text.Split('/');
                if (maso[0] == "MH")
                {
                    MuaHang muahang = (from mh in GetAllMuaHang()
                                       where mh.MaSo == e.ObjectInfo.Text
                                       select mh).FirstOrDefault();

                    FrmMuaHang frmMuaHang = new FrmMuaHang(muahang, nhanvien);
                    this.Hide();
                    frmMuaHang.ShowDialog();
                }
                else if (maso[0] == "PC")
                {
                    PhieuChiTien phieuchitien = (from c in GetAllPhieuChiTien()
                                                 where c.MaSo == e.ObjectInfo.Text
                                                 select c).FirstOrDefault();

                    FrmPhieuChiTien frmPhieuChiTien = new FrmPhieuChiTien(phieuchitien, this.nhanvien);
                    this.Hide();
                    frmPhieuChiTien.ShowDialog();
                }
                else if (maso[0] == "PT")
                {
                    PhieuThuTien phieuthutien = (from c in GetAllPhieuThuTien()
                                                 where c.MaSo == e.ObjectInfo.Text
                                                 select c).FirstOrDefault();

                    FrmPhieuThuTien frmPhieuThuTien = new FrmPhieuThuTien(phieuthutien, this.nhanvien);
                    this.Hide();
                    frmPhieuThuTien.ShowDialog();
                }
                else if (maso[0] == "PKT")
                {
                    PhieuKeToan phieuketoan = (from p in GetAllPhieuKeToan()
                                               where p.MaSo == e.ObjectInfo.Text
                                               select p).FirstOrDefault();

                    FrmPhieuKeToan frmPhieuKeToan = new FrmPhieuKeToan(phieuketoan);
                    this.Hide();
                    frmPhieuKeToan.ShowDialog();
                }
                else if (maso[0] == "PCK")
                {
                    PhieuChuyenKho phieuchuyenkho = (from p in GetAllPhieuChuyenKho()
                                                     where p.MaSo == e.ObjectInfo.Text
                                                     select p).FirstOrDefault();

                    FrmPhieuChuyenKho frmPhieuChuyenKho = new FrmPhieuChuyenKho();
                    frmPhieuChuyenKho.NhanVien = this.nhanvien;
                    frmPhieuChuyenKho.mphieuchuyenkho = phieuchuyenkho;
                    frmPhieuChuyenKho.isEdited = true;
                    this.Hide();
                    frmPhieuChuyenKho.ShowDialog();
                }
                else if (maso[0] == "PDH")
                {
                    PhieuDoiHang phieudoihang = (from p in GetAllPhieuDoiHang()
                                                 where p.MaSo == e.ObjectInfo.Text
                                                 select p).FirstOrDefault();

                    FrmPhieuDoiHang frmPhieuDoiHang = new FrmPhieuDoiHang(phieudoihang);
                    this.Hide();
                    frmPhieuDoiHang.ShowDialog();
                }
                else if (maso[0] == "PHT")
                {
                    PhieuHangTra phieuhangtra = (from p in GetAllPhieuHangTra()
                                                 where p.MaSo == e.ObjectInfo.Text
                                                 select p).FirstOrDefault();

                    FrmPhieuHangTra frmPhieuHangTra = new FrmPhieuHangTra(phieuhangtra);
                    this.Hide();
                    frmPhieuHangTra.ShowDialog();
                }
                else if (maso[0] == "PNK")
                {
                    PhieuNhapKho phieunhapkho = (from p in GetAllPhieuNhapKho()
                                                 where p.MaSo == e.ObjectInfo.Text
                                                 select p).FirstOrDefault();

                    FrmPhieuNhapKho frmPhieuNhapKho = new FrmPhieuNhapKho(phieunhapkho, this.nhanvien);
                    this.Hide();
                    frmPhieuNhapKho.ShowDialog();
                }
                else if (maso[0] == "PXH")
                {
                    PhieuXuatHang phieuxuathang = (from p in GetAllPhieuXuatHang()
                                                   where p.MaSo == e.ObjectInfo.Text
                                                   select p).FirstOrDefault();

                    List<CTPhieuXuatHang> lstCTphieuxuathang = (from ctpxh in GetAllCTPhieuXuatHang()
                                                                where ctpxh.IdPhieuXuatHang == phieuxuathang.Id
                                                                select ctpxh).ToList();

                    FrmPhieuXuatHang frmPhieuXuatHang = new FrmPhieuXuatHang();
                    frmPhieuXuatHang.NhanVien = this.nhanvien;
                    frmPhieuXuatHang.PhieuXuatHang = phieuxuathang;
                    frmPhieuXuatHang.ListCTphieuxuathang = lstCTphieuxuathang;
                    frmPhieuXuatHang.IsEdited = true;
                    frmPhieuXuatHang.isPrinting = false;
                }
                else if (maso[0] == "PXK")
                {
                    PhieuXuatKho pxk = (from p in GetAllPhieuXuatKho()
                                        where p.MaSo == e.ObjectInfo.Text
                                        select p).FirstOrDefault();

                    FrmPhieuXuatKho px = new FrmPhieuXuatKho(this.nhanvien, pxk);
                    this.Hide();
                    px.EditButtonClick = 1;
                    px.ShowDialog();
                }
                else if (maso[0] == "UNC")
                {
                    UyNhiemChi uynhiemchi = (from u in GetAllUyNhiemChi()
                                             where u.MaSo == e.ObjectInfo.Text
                                             select u).FirstOrDefault();

                    FrmUyNhiemChi frmUyNhiemChi = new FrmUyNhiemChi(uynhiemchi, this.nhanvien);
                    this.Hide();
                    frmUyNhiemChi.ShowDialog();
                }
            }
            else if (e.ObjectInfo.Name == "SoChungTuNhap" && cnt == 11)
            {
                string[] maso = e.ObjectInfo.Text.Split('/');
                if (maso[0] == "PCK")
                {
                    PhieuChuyenKho phieuchuyenkho = (from p in GetAllPhieuChuyenKho()
                                                     where p.MaSo == e.ObjectInfo.Text
                                                     select p).FirstOrDefault();

                    FrmPhieuChuyenKho frmPhieuChuyenKho = new FrmPhieuChuyenKho();
                    frmPhieuChuyenKho.NhanVien = this.nhanvien;
                    frmPhieuChuyenKho.mphieuchuyenkho = phieuchuyenkho;
                    frmPhieuChuyenKho.isEdited = true;
                    this.Hide();
                    frmPhieuChuyenKho.ShowDialog();
                }
                else if (maso[0] == "PDH")
                {
                    PhieuDoiHang phieudoihang = (from p in GetAllPhieuDoiHang()
                                                 where p.MaSo == e.ObjectInfo.Text
                                                 select p).FirstOrDefault();

                    FrmPhieuDoiHang frmPhieuDoiHang = new FrmPhieuDoiHang(phieudoihang);
                    this.Hide();
                    frmPhieuDoiHang.ShowDialog();
                }
                else if (maso[0] == "PXH")
                {
                    PhieuXuatHang phieuxuathang = (from p in GetAllPhieuXuatHang()
                                                   where p.MaSo == e.ObjectInfo.Text
                                                   select p).FirstOrDefault();

                    List<CTPhieuXuatHang> lstCTphieuxuathang = (from ctpxh in GetAllCTPhieuXuatHang()
                                                                where ctpxh.IdPhieuXuatHang == phieuxuathang.Id
                                                                select ctpxh).ToList();

                    FrmPhieuXuatHang frmPhieuXuatHang = new FrmPhieuXuatHang();
                    frmPhieuXuatHang.NhanVien = this.nhanvien;
                    frmPhieuXuatHang.PhieuXuatHang = phieuxuathang;
                    frmPhieuXuatHang.ListCTphieuxuathang = lstCTphieuxuathang;
                    frmPhieuXuatHang.IsEdited = true;
                    frmPhieuXuatHang.isPrinting = false;
                }
                else if (maso[0] == "PXK")
                {
                    PhieuXuatKho pxk = (from p in GetAllPhieuXuatKho()
                                        where p.MaSo == e.ObjectInfo.Text
                                        select p).FirstOrDefault();

                    FrmPhieuXuatKho px = new FrmPhieuXuatKho(this.nhanvien, pxk);
                    this.Hide();
                    px.EditButtonClick = 1;
                    px.ShowDialog();
                }
            }
            else if (e.ObjectInfo.Name == "SoChungTuXuat" && cnt == 11)
            {
                string[] maso = e.ObjectInfo.Text.Split('/');
                if (maso[0] == "MH")
                {
                    MuaHang muahang = (from mh in GetAllMuaHang()
                                       where mh.MaSo == e.ObjectInfo.Text
                                       select mh).FirstOrDefault();

                    FrmMuaHang frmMuaHang = new FrmMuaHang(muahang, nhanvien);
                    this.Hide();
                    frmMuaHang.ShowDialog();
                }
                else if (maso[0] == "PHT")
                {
                    PhieuHangTra phieuhangtra = (from p in GetAllPhieuHangTra()
                                                 where p.MaSo == e.ObjectInfo.Text
                                                 select p).FirstOrDefault();

                    FrmPhieuHangTra frmPhieuHangTra = new FrmPhieuHangTra(phieuhangtra);
                    this.Hide();
                    frmPhieuHangTra.ShowDialog();
                }
                else if (maso[0] == "PNK")
                {
                    PhieuNhapKho phieunhapkho = (from p in GetAllPhieuNhapKho()
                                                 where p.MaSo == e.ObjectInfo.Text
                                                 select p).FirstOrDefault();

                    FrmPhieuNhapKho frmPhieuNhapKho = new FrmPhieuNhapKho(phieunhapkho, this.nhanvien);
                    this.Hide();
                    frmPhieuNhapKho.ShowDialog();
                }
            }

            formclosexemreport(true);
            this.Close();
        }

        public List<PhieuChiTien> GetAllPhieuChiTien()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = phieuchitienMgt.GetAllPhieuChiTien();
            List<PhieuChiTien> listPhieuChiTien = (List<PhieuChiTien>)resultObj.Result;
            return listPhieuChiTien;
        }

        public List<PhieuThuTien> GetAllPhieuThuTien()
        {
            ResultObj result = new ResultObj();
            result = phieuthutienMgt.GetAllPhieuThuTien();
            List<PhieuThuTien> lstPhieuthutien = (List<PhieuThuTien>)result.Result;
            return lstPhieuthutien;
        }

        public List<MuaHang> GetAllMuaHang()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = muahangMgt.GetAllMuaHang();
            List<MuaHang> listMuaHang = (List<MuaHang>)resultObj.Result;
            return listMuaHang;
        }

        public List<PhieuKeToan> GetAllPhieuKeToan()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = phieuketoanMgt.GetAllPhieuKeToan();
            List<PhieuKeToan> listPhieuKeToan = (List<PhieuKeToan>)resultObj.Result;
            return listPhieuKeToan;
        }

        public List<PhieuChuyenKho> GetAllPhieuChuyenKho()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = phieuchuyenkhoMgt.GetAllPhieuChuyenKho();
            List<PhieuChuyenKho> listPhieuChuyenKho = (List<PhieuChuyenKho>)resultObj.Result;
            return listPhieuChuyenKho;
        }

        public List<PhieuDoiHang> GetAllPhieuDoiHang()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = phieudoihangMgt.GetAllPhieuDoiHang();
            List<PhieuDoiHang> listPhieuDoiHang = (List<PhieuDoiHang>)resultObj.Result;
            return listPhieuDoiHang;
        }

        public List<PhieuHangTra> GetAllPhieuHangTra()
        {
            ResultObj result = new ResultObj();
            result = phieuhangtraMgt.GetAllPhieuHangTra();
            List<PhieuHangTra> lstPhieuHangTra = (List<PhieuHangTra>)result.Result;
            return lstPhieuHangTra;
        }

        public List<PhieuNhapKho> GetAllPhieuNhapKho()
        {
            ResultObj result = new ResultObj();
            result = phieunhapkhoMgt.GetAllPhieuNhapKho();
            List<PhieuNhapKho> lstPhieuNhapKho = (List<PhieuNhapKho>)result.Result;
            return lstPhieuNhapKho;
        }

        public List<PhieuXuatHang> GetAllPhieuXuatHang()
        {
            ResultObj result = new ResultObj();
            result = phieuxuathangMgt.GetAllPhieuXuatHang();
            List<PhieuXuatHang> lstPhieuXuatHang = (List<PhieuXuatHang>)result.Result;
            return lstPhieuXuatHang;
        }

        public List<CTPhieuXuatHang> GetAllCTPhieuXuatHang()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = ctphieuxuathangMgt.GetAllCTPhieuXuatHang();
            List<CTPhieuXuatHang> listCTPhieuXuatHang = (List<CTPhieuXuatHang>)resultObj.Result;
            return listCTPhieuXuatHang;
        }

        public List<PhieuXuatKho> GetAllPhieuXuatKho()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = PhieuXuatKhoMgt.GetAllPhieuXuatKho();
            List<PhieuXuatKho> listPhieuXuatKho = (List<PhieuXuatKho>)resultObj.Result;
            return listPhieuXuatKho;
        }

        public List<UyNhiemChi> GetAllUyNhiemChi()
        {
            ResultObj result = new ResultObj();
            result = uynhiemchiMgt.GetAllUyNhiemChi();
            List<UyNhiemChi> lstUyNhiemChi = (List<UyNhiemChi>)result.Result;
            return lstUyNhiemChi;
        }
    }
}

